package model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Process {
	int id;
	int service;
	int processMoveCost;
	HashMap<String, Integer> requirements;
	String runsOnMachine;

	List<Integer> eligibleMachines;

	public Process() {
		requirements = new HashMap<String, Integer>();
		eligibleMachines = new ArrayList<Integer>();
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setService(int service_id) {
		this.service = service_id;
	}

	public int getService() {
		return service;
	}

	public void addRequirement(String resource, int value) {
		requirements.put(resource, value);
	}

	public void setProcessMoveCost(int x) {
		processMoveCost = x;
	}

	public int getProcessMoveCost() {
		return processMoveCost;
	}

	public List<Integer> getEligibleMachines() {
		return eligibleMachines;
	}

	public void setEligibleMachines(List<Integer> eligibleMachines) {
		this.eligibleMachines = eligibleMachines;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(String.format("ID=%-5s S=%-5d PMC=%-2d REQUIREMENTS=", id, service,
				processMoveCost));
		for (String key : requirements.keySet()) {
			sb.append(String.format("%s->%,10d\t", key, requirements.get(key)));
		}
		return sb.toString();
	}

	public int getRequirementFor(String r_i_s) {
		return requirements.get(r_i_s);
	}

	public void addEligibleMachine(Integer machine_id) {
		if (!eligibleMachines.contains(machine_id)) {
			eligibleMachines.add(machine_id);
		}
	}

	public void removeElligibleMachine(Integer m_id) {
		if (eligibleMachines.contains(m_id)) {
			eligibleMachines.remove(m_id);
		}
	}

	public void removeElligibleMachines(List<Integer> m_ids_to_be_removed) {
		for (Integer m_id : m_ids_to_be_removed) {
			removeElligibleMachine(m_id);
		}

	}

	public String getEligibleMachinesInfo() {
		StringBuilder sb = new StringBuilder();
		for (Integer m_id : eligibleMachines) {
			sb.append(String.format("%d ", m_id));
		}
		if (sb.length() != 0)
			sb.deleteCharAt(sb.length() - 1);
		return String.format("[p%d] machines=[%s]", id, sb.toString());
	}

	public void clearEligibleMachines() {
		eligibleMachines = new ArrayList<Integer>();
	}

}
